package com.artron.mediaartron.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import com.artron.baselib.base.BaseFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.base.BaseLinkPage;
import com.artron.mediaartron.data.production.SuitableSizeHelper;
import com.artron.mediaartron.ui.fragment.dialog.NormalDialogFragment;
import com.artron.mediaartron.ui.fragment.made.CustomSelectionFragment;
import com.artron.mediaartron.ui.fragment.made.picture.ScaleMedia;
import com.artron.mediaartron.ui.linkpage.CustomSelectionPosterLinkPage;
import com.artron.mediaartron.ui.linkpage.CustomSelectionLinkPage;
import com.artron.mediaartron.util.SharePreferenceUtils;
import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.entity.BaseMedia;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import rx.functions.Action0;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-21.
 */

public class CustomSelectionActivity extends AppBaseActivity {

    private static final String TAG = "CustomSelectionActivity";

    public static final String TYPE = "Type";

    @IntDef({CUSTOM_MADE, POSTER, LITTLE_TIME, PICK_UP_LIGHT_SET})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Type{}

    public static final int CUSTOM_MADE = 1000;
    public static final int POSTER = 1001;
    public static final int LITTLE_TIME = 1002;
    public static final int PICK_UP_LIGHT_SET = 1003;

    public static void start(Context context, @Type int type) {
        Intent starter = new Intent(context, CustomSelectionActivity.class);
        starter.putExtra(TYPE, type);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        int type = getIntent().getIntExtra(TYPE, 1000);
        switch (type) {
            case CUSTOM_MADE:
                mTvTitle.setText("框画定制");
                break;
            case POSTER:
                mTvTitle.setText("海报定制");
                break;
            case LITTLE_TIME:
                break;
            case PICK_UP_LIGHT_SET:
                break;

        }
        BaseFragment fragment = CustomSelectionFragment.newInstance(type);

        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments == null || fragments.size() == 0) {
            addFragment(R.id.General_content,fragment, false);
        } else {
            replaceFragment(R.id.General_content, fragment, false);
        }

    }

    @Override
    protected View initLinkPageView(ViewGroup parent) {
        int type = getIntent().getIntExtra(TYPE, 1000);
        BaseLinkPage linkPage;
        String preferenceKey;
        if (type == CUSTOM_MADE) {
            preferenceKey = "Custom_Made";
            linkPage = new CustomSelectionLinkPage(parent);
        } else {
            preferenceKey = "Poster";
            linkPage = new CustomSelectionPosterLinkPage(parent);
        }

        boolean isFirst = SharePreferenceUtils.getInstance().getBoolean(preferenceKey, true);
        if (isFirst) {
            SharePreferenceUtils.getInstance().putBoolean(preferenceKey, false);
            return linkPage.getContentView();
        } else {
            return super.initLinkPageView(parent);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_selection, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void initActionBar() {
        mToolbar.setNavigationIcon(R.drawable.ic_back);
        super.initActionBar();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ArrayList<BaseMedia> result = Boxing.getResult(data);
        if (result != null && result.size() > 0) {
            BaseMedia baseMedia = result.get(0);
            double scale = 0;
            if (baseMedia instanceof ScaleMedia) {
                scale = ((ScaleMedia) baseMedia).getScale();
            }

            ((CustomSelectionFragment) mCurrentFragment)
                    .updateChildImageViewContent(baseMedia.getId() ,baseMedia.getPath(), scale);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SuitableSizeHelper.getHelper().release();
    }

    @Override
    public void onBackPressed() {
        NormalDialogFragment dialog = NormalDialogFragment.newInstance("是否放弃编辑", "否", "是");
        dialog.setOnNegativeClickListener(new Action0() {
            @Override
            public void call() {
                toBackActivity();
            }
        });
        dialog.show(getSupportFragmentManager(), dialog.getOurTag());
    }
}
